<?php

/**
 * page actions.
 *
 * @package    blogallery
 * @subpackage page
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 9301 2008-05-27 01:08:46Z dwhittle $
 */
class pageActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex($request)
  {
    $this->recent = PostPeer::retrieveRecent();
  }
  
  public function executeShow($request)
  {
  	$this->page = PagePeer::retrieveByHandle($request->getParameter('handle'));
  	$this->forward404Unless($this->page instanceof Page);
  }
  
  public function executeCreate()
  {
  	$this->form = new PageForm();
  	
  	$this->getResponse()->addJavascript('livepipe.js', 'last');
  	$this->getResponse()->addJavascript('tabs.js', 'last'); 
    $this->setTemplate('edit');
  }
  
  public function executeEdit($request)
  {
  	$page = PagePeer::retrieveByPK($request->getParameter('id'));
  	$this->forward404Unless($page instanceof Page);
  	
  	$this->form = new PageForm($page);
  	
  	$this->getResponse()->addJavascript('livepipe.js', 'last');
  	$this->getResponse()->addJavascript('tabs.js', 'last');
  }
  
  public function executeUpdate($request)
  {
  	if ($request->isMethod('post'))
  	{  	  
      if ($request->getParameter('page[id]'))
      {
        $page = PagePeer::retrieveByPk($request->getParameter('page[id]'));
        $this->forward404Unless($page instanceof Page);
      }
      else
      {
        $page = new Page();
      }
      
  	  $form = new PageForm($page);
  	  $form->bind($request->getParameter('page'));
  	  $form->save();
  	  
      return $this->redirect('@page?lang='.$this->getUser()->getLang().'&handle='.$page->getHandle());
  	}
  	
  	return $this->redirect('@default?module=page&action=edit&id='.$this->getRequestParameter('id'));  	
  }
  
  public function executeDelete($request)
  {
  	$page = PagePeer::retrieveByPK($request->getParameter('id'));
  	$this->forward404Unless($page instanceof Page);
  	
  	$page->delete();
  	
    return sfView::NONE;
  }
}
